﻿package mortal.game.view.petNew.view
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.rotate3dModelSprite.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.playerSystem.view.*;
    import mortal.game.view.rebate.openServerSevenDay.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class PetActivityPanel extends Window
    {
        private var _bg:GBitmap;
        private var _titleTxt:GTextFiled;
        private var _levelTxt:GTextFiled;
        private var _levelName:GTextFiled;
        private var _combat:BitmapNumberText;
        private var _returnText:GTextFiled;
        private var _canTakeImg:GBitmap;
        private var _propItem:BaseItem;
        private var _takeBtn:GButton;
        private var _rect3d:Rect3DObject;
        private var _img2d:Img2D;
        private var _bodyPlayer:ActionPlayer;
        private var _rotate3d:Rotate3dModelSprite;
        private var _hideCallBack:Function;
        private var _isRoleModelDisposed:Boolean = true;
        private var _isWeaponModelDisposed:Boolean = true;
        private var _frameTimer:FrameTimer;
        private var _config:TMountConfig;
        private static var _instance:PetActivityPanel;

        public function PetActivityPanel(param1:ILayer = null)
        {
            super(param1);
            isForbidenDrag = true;
            this.layer = LayerManager.windowLayer3D;
            this.setSize(352, 512);
            return;
        }// end function

        private function init3DRect() : void
        {
            this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(8, 35 - 5, 335, 469), this);
            Rect3DManager.instance.windowShowHander(null, this);
            return;
        }// end function

        private function add3dBg() : void
        {
            if (this._rect3d)
            {
                this._rect3d.removeImg(this._img2d);
                this._img2d = new Img2D(null, this._bg.bitmapData, new Rectangle(0, 0, 335, 469), false);
                this._rect3d.addImg(this._img2d);
            }
            return;
        }// end function

        private function initModel() : void
        {
            if (this._rect3d)
            {
                this.updateModel();
            }
            return;
        }// end function

        private function updateAll() : void
        {
            this.updateLevelInfo();
            this.updateCombat();
            this.updateReturnInfo();
            if (this._bodyPlayer != null)
            {
                this.updateModel();
            }
            return;
        }// end function

        private function updateLevelInfo() : void
        {
            this._levelTxt.text = Language.getStringByParam(71728, this._config.code);
            this._levelName.text = this._config.name;
            return;
        }// end function

        private function updateCombat() : void
        {
            var _loc_2:Array = null;
            var _loc_3:Array = null;
            var _loc_4:String = null;
            var _loc_1:Number = 0;
            if (this._config)
            {
                _loc_2 = [];
                _loc_3 = AttributeUtil.attrs.concat();
                _loc_3.splice(2, 1);
                _loc_3.splice(-1);
                for each (_loc_4 in _loc_3)
                {
                    
                    _loc_1 = _loc_1 + CombatUtil.getCombatValueByName(_loc_4, this._config[_loc_4]);
                }
            }
            this._combat.text = int(_loc_1) + "";
            return;
        }// end function

        private function updateReturnInfo(param1:Object = null) : void
        {
            var _loc_3:Array = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:String = null;
            var _loc_7:ItemData = null;
            var _loc_8:int = 0;
            var _loc_2:* = this._config.returnAdvanceNum;
            if (_loc_2)
            {
                _loc_3 = _loc_2.split("#");
                _loc_4 = int(_loc_3[0]);
                _loc_5 = int(_loc_3[1]);
                _loc_6 = HTMLUtil.addColor(_loc_5 + "", GlobalStyle.colorLv);
                this._returnText.htmlText = Language.getStringByParam(71729, _loc_6);
                this._propItem.itemData = new ItemData(_loc_4);
                this._propItem.amount = _loc_5;
                if (Cache.instance.newPet.pet.advanceReturn == 0 && Cache.instance.newPet.pet.code == 3)
                {
                    this._takeBtn.mouseEnabled = true;
                    this._canTakeImg.visible = true;
                    this._takeBtn.filters = null;
                    EffectManager.glowFilterReg(this._takeBtn);
                    _loc_7 = this._propItem.itemData;
                    _loc_8 = this._propItem.amount;
                    hide();
                    if (PlayerSystemAdvanceReturnWin.instance.isHide)
                    {
                        PlayerSystemAdvanceReturnWin.instance.show();
                        PlayerSystemAdvanceReturnWin.instance.addRewardItem(_loc_7, _loc_8);
                    }
                }
                else
                {
                    EffectManager.glowFilterUnReg(this._takeBtn);
                    this._takeBtn.mouseEnabled = false;
                    this._canTakeImg.visible = false;
                    this._takeBtn.filters = [FilterConst.colorFilter2];
                }
            }
            this._canTakeImg.visible = false;
            return;
        }// end function

        private function updateModel() : void
        {
            var _loc_1:* = PetUtil.getPetModel(1, this._config.modelId);
            var _loc_2:* = _loc_1.mesh + ".md5mesh";
            var _loc_3:* = _loc_1.bone + ".skeleton";
            if (this._bodyPlayer && this._bodyPlayer.meshUrl == _loc_2 && this._bodyPlayer.animUrl == _loc_3)
            {
                return;
            }
            if (this._bodyPlayer)
            {
                this._rect3d.disposeObj3d(this._bodyPlayer);
            }
            this._bodyPlayer = FrObjectPool.getObject(ActionPlayer, this);
            this._bodyPlayer.changeAction(ActionName.Stand);
            this._bodyPlayer.hangBoneName = BoneConst.MountHangModelBone;
            this._bodyPlayer.selectEnabled = true;
            this._bodyPlayer.play();
            this._bodyPlayer.setRotation(0, 0, 0);
            this._bodyPlayer.scaleValue = 2;
            this._bodyPlayer.load(_loc_2, _loc_3, _loc_1.texture, this._rect3d.renderList);
            this._rect3d.addObject3d(this._bodyPlayer, 170, 360);
            PetSelfEffectUtil.addSelfEffect(this._bodyPlayer, _loc_1.effect, this._rect3d.renderList);
            this._rotate3d.model = this._bodyPlayer;
            return;
        }// end function

        private function removeRoleModel() : void
        {
            this._rect3d.disposeObj3d(this._bodyPlayer);
            this._bodyPlayer = null;
            this._isRoleModelDisposed = true;
            return;
        }// end function

        private function onTakeHandler(event:MouseEvent) : void
        {
            if (this._config != null)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetAdvanceRewardGet, this._config.code));
            }
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            if (OpenServerSevenDayUtil.isNotOpenSpeActive())
            {
                return;
            }
            super.show(param1, param2);
            this.updateAll();
            return;
        }// end function

        override public function set y(param1:Number) : void
        {
            super.y = param1;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            _isAddClolseButton = false;
            super.createDisposedChildrenImpl();
            this.init3DRect();
            this._config = MountConfig.instance.getFashionInfoByCode(EAdvanceComponentType._EAdvanceComponentTypePet, 3);
            this._bg = UIFactory.gBitmap("");
            this._rotate3d = UIFactory.getUICompoment(Rotate3dModelSprite, 0, 0, this.contentTopOf3DSprite);
            this._rotate3d.setSize(335, 469);
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.ActivityBg_Fashion, 0, 0, this));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.FashionActiveText, 20 - 28, 320 + 16, this.contentTopOf3DSprite));
            this._titleTxt = UIFactory.gTextField("" + Language.getString(71730), 153 + 2, 4, 70, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatAnjin);
            this._levelTxt = UIFactory.gTextField("", 50, 40, 54, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatBai);
            this._levelName = UIFactory.gTextField("", 98, 41, 70, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatBai);
            this._combat = UIFactory.bitmapNumberText(193 + 50 + 6, 45 - 4, "PetGrowYellowNum.png", 18, 21, -6, this.contentTopOf3DSprite, 9);
            this._returnText = UIFactory.gTextField("", 141 - 20, 455 + 19, 180, 20, this.contentTopOf3DSprite);
            this._canTakeImg = UIFactory.gBitmap(ImagesConst.ItemGetIcon, 260 + 40, 324 + 72, this.contentTopOf3DSprite);
            this._propItem = UICompomentPool.getUICompoment(BaseItem);
            this._propItem.setItemStyle(ItemStyleConst.Small, ImagesConst.FashionEquipBg, 9, 9);
            this._propItem.isDragAble = false;
            this._propItem.isShowColorBg = false;
            UIFactory.setObjAttri(this._propItem, 257 + 16, 405 + 20 - 4, -1, -1, contentTopOf3DSprite);
            this._takeBtn = UIFactory.gButton("" + Language.getString(71731), 264 + 15, 450 + 25, 46, 21, this.contentTopOf3DSprite);
            this._takeBtn.configEventListener(MouseEvent.CLICK, this.onTakeHandler);
            LoaderManager.instance.load(ImagesConst.ActivityBg2_Fashion + ".swf", this.onBgLoadCompl);
            NetDispatcher.addCmdListener(ServerCommand.NewPetAttributeUpdate, this.updateReturnInfo);
            this._takeBtn.visible = false;
            return;
        }// end function

        private function onBgLoadCompl(param1) : void
        {
            if (isDisposed)
            {
                return;
            }
            this._bg.bitmapData = param1.bitmapData as BitmapData;
            this.add3dBg();
            this.initModel();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl();
            NetDispatcher.removeCmdListener(ServerCommand.NewPetAttributeUpdate, this.updateReturnInfo);
            EffectManager.glowFilterUnReg(this._takeBtn);
            this._titleTxt.dispose();
            this._levelTxt.dispose();
            this._levelName.dispose();
            this._combat.dispose();
            this._bg.dispose();
            this._rotate3d.dispose();
            this._returnText.dispose();
            this._canTakeImg.dispose();
            this._propItem.dispose();
            this._takeBtn.dispose();
            if (this._frameTimer)
            {
                this._frameTimer.dispose();
            }
            this._titleTxt = null;
            this._levelTxt = null;
            this._levelName = null;
            this._combat = null;
            this._bg = null;
            this._rotate3d = null;
            this._returnText = null;
            this._frameTimer = null;
            this._canTakeImg = null;
            this._propItem = null;
            this._takeBtn = null;
            this._isRoleModelDisposed = true;
            this._isWeaponModelDisposed = true;
            if (this._rect3d)
            {
                if (this._img2d)
                {
                    this._rect3d.removeImg(this._img2d);
                    this._img2d = null;
                }
                if (this._bodyPlayer)
                {
                    this._rect3d.disposeObj3d(this._bodyPlayer);
                    this._bodyPlayer = null;
                }
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            return;
        }// end function

        override protected function onWindowDownHandler(event:MouseEvent) : void
        {
            return;
        }// end function

        public static function get instance() : PetActivityPanel
        {
            if (_instance == null)
            {
                _instance = new PetActivityPanel;
            }
            return _instance;
        }// end function

        public static function hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

        public static function isViewShow() : Boolean
        {
            return _instance != null && !_instance.isHide;
        }// end function

    }
}
